package com.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.common.constant.Constant;
import com.common.utils.DateUtil;
import com.common.utils.StringUtil;
import com.entity.Card;
import com.entity.Hint;
import com.entity.Node;
import com.mapper.HintMapper;
import com.service.CardService;
import com.service.HintService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.service.NodeService;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 *
 * @author wty
 * @since 2022-01-07
 */
@Service
public class HintServiceImpl extends ServiceImpl<HintMapper, Hint> implements HintService {

    @Autowired
    private NodeService nodeService;

    @Autowired
    private CardService cardService;

    @Override
    public void send() {

        Wrapper<Node> wrnode=new EntityWrapper<>();
        List<Node> nodes = nodeService.selectList(wrnode);

        //判断现在的时间，把属于这个时间段的习惯筛选出来
        String nowTime = DateFormatUtils.format(new Date(), "HHmm");//2018
        String h = nowTime.substring(0,2);//现在几点
        String s = nowTime.substring(2,4);//现在多少分
        Integer index = Integer.valueOf(h);

        for (Node node : nodes) {
            String executeTime = node.getExecuteTime();
            List<Integer> executeList = JSONArray.parseArray(executeTime, Integer.class);

            Integer executeIndex = executeList.get(index);
            if( executeIndex<1){
                continue;
            }
            //获取需要通知的卡片
            String cardId = node.getCardId();
            if(StringUtil.isEmpty(cardId)){
                continue;
            }
            List<String> cardIds = JSONArray.parseArray(cardId, String.class);
            Wrapper<Card> cardWr=new EntityWrapper<>();
            cardWr.in("id",cardIds);
            List<Card> cards = cardService.selectList(cardWr);
            if(CollectionUtils.isEmpty(cards)){
                continue;
            }
            //进行通知

            //判断这个任务今天还需要通知么。
            String nodeInformTime = Constant.IS_INFORM_MAP.get(node.getId());
            if(!StringUtil.isEmpty(nodeInformTime)&&nodeInformTime.equals(DateUtil.getDate_yyyyMMdd())){
                continue;
            }
            //通知类型，多少分钟通知一次，
            Long aLong = Constant.INFORM_MAP.get(node.getId());
            if(null==aLong){
                Constant.INFORM_MAP.put(node.getId(),DateUtil.getLongByCurrTime());
            }else{
                Long longByCurrTime = DateUtil.getLongByCurrTime();
                if(longByCurrTime-aLong>executeIndex*60){//多少分钟通知一次
                    nodeService.doSend(cards,node);
                    Constant.INFORM_MAP.put(node.getId(),DateUtil.getLongByCurrTime());
                }
            }
        }
    }
}
